/* iterator迭代器 */

const arr = [1, 2, 3]
const iterator = arr[Symbol.iterator]() // 可遍历数据都有一个Symbol[iterator]属性
console.log(iterator.next())
console.log(iterator.next())
console.log(iterator.next())
console.log(iterator.next())

// 实现迭代器让for...of来遍历对象
const obj = {
  life: ['打游戏', '看书', '喝茶'],
  learn: ['js', 'css', 'html'],
  work: ['study']
}
obj[Symbol.iterator] = function () {
  const keys = Object.keys(this)
  let count = 0
  return {
    next () {
      return { value: [keys[count], obj[keys[count]]], done: count++ >= keys.length }
    }
  }
}
const o = obj[Symbol.iterator]()
console.log(o.next())
console.log(o.next())
console.log(o.next())
console.log(o.next())

for (const [key, val] of obj) {
  console.log(key, val)
}